//
// Copyright (C) JV Software 
// 
// File: pluginMain.cpp
//
// Author: Maya Plug-in Wizard 2.0
//

#define MAYA_PLUGIN_MAIN

#include <maya/MFnPlugin.h>
#include "FallenPlugin.h"

FPluginMenu imageManagerMenu( TEXT( "FallenTools" ), 1, TEXT( "Convert an Image..." ), TEXT("FImageDialogCmd") );

MFnPlugin *MayaPlugin;

FImageDialogCmd			*imageDialogCmd;

HMODULE		g_moduleHandle = NULL;

FArray<MString>			cmds;
bool isShutdown = false;

//
// Maya_RegisterPlugin
//
void Maya_RegisterPlugin( MString name, void* (*creator)() ) {
	for(int i = 0; i < cmds.size(); i++ ) {
		if(cmds[i] == name)
			return;
	}

	cmds.push_back( name );
	MayaPlugin->registerCommand( name, creator );
}

//
// Maya_DeregisterCommand
//
void Maya_DeregisterCommand( MString name ) {
	if(!isShutdown) 
		return;

	MayaPlugin->deregisterCommand( name );
}

//
// GetLocalModuleHandle
//
HMODULE GetLocalModuleHandle( void ) {
	// http://www.dotnet247.com/247reference/msgs/13/65259.aspx
	if(g_moduleHandle == NULL) {
		MEMORY_BASIC_INFORMATION mbi;
		static int dummy;
		VirtualQuery( &dummy, &mbi, sizeof(mbi) );
		DWORD hMod = (DWORD)mbi.AllocationBase;
		char szModule[MAX_PATH];
		GetModuleFileNameA( (HMODULE)hMod, szModule, sizeof(szModule) );

		g_moduleHandle = GetModuleHandleA( szModule );
	}

	return g_moduleHandle;
}

MStatus initializePlugin( MObject obj )
//
//	Description:
//		this method is called when the plug-in is loaded into Maya.  It 
//		registers all of the services that this plug-in provides with 
//		Maya.
//
//	Arguments:
//		obj - a handle to the plug-in object (use MFnPlugin to access it)
//
{ 
	MStatus   status;
	MFnPlugin *plugin = new MFnPlugin( obj, "JV Software", "2012", "Any");

	MayaPlugin = plugin;

	imageDialogCmd = new FImageDialogCmd();

	return status;
}

MStatus uninitializePlugin( MObject obj )
//
//	Description:
//		this method is called when the plug-in is unloaded from Maya. It 
//		deregisters all of the services that it was providing.
//
//	Arguments:
//		obj - a handle to the plug-in object (use MFnPlugin to access it)
//
{
	MStatus   status;
	MFnPlugin plugin( obj );

	isShutdown = true;

	FPluginDialog::DestroyAllDialogs();

	delete imageDialogCmd;

	return status;
}
